#include <gtk/gtk.h>
#include <string.h>
#include "common.h"

#define TITLE "toolbar"

void undo_redo(GtkWidget *widget, gpointer item)
{
    static int count = 2;

    const char *name = gtk_widget_get_name(widget);

    if ( strncmp(name, "undo", 4) ) {
        ++count;
    } else {
        --count;
    }

    if ( count < 0 || count > 5) {
        gtk_widget_set_sensitive(widget, FALSE);
    }
    gtk_widget_set_sensitive(item, TRUE);
}

int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *toolbar;
    GtkToolItem *undoToolButton;
    GtkToolItem *redoToolButton;
    GtkToolItem *sepToolItem;
    GtkToolItem *exitToolButton;

//    GdkColor  color_black_ = {0x0, 0x0, 0x0, 0x0};

    gtk_init(&argc,&argv);

    /* create the main, top level, window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), TITLE);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
//    gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color_black_); /*设置为黑色背景*/

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    /*toolbar component*/
    toolbar = gtk_toolbar_new();
    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
    gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);

    /*undo component*/
    undoToolButton = gtk_tool_button_new_from_stock(GTK_STOCK_UNDO);
    gtk_widget_set_name(GTK_WIDGET(undoToolButton), "undo");
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), undoToolButton, -1);

    /*redo component*/
    redoToolButton = gtk_tool_button_new_from_stock(GTK_STOCK_REDO);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), redoToolButton, -1);

    /*separator*/
    sepToolItem = gtk_separator_tool_item_new();
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), sepToolItem, -1);

    /*exit component*/
    exitToolButton = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exitToolButton, -1);


    gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 5);

    g_signal_connect(G_OBJECT(undoToolButton), "clicked", G_CALLBACK(undo_redo), redoToolButton);
    g_signal_connect(G_OBJECT(redoToolButton), "clicked", G_CALLBACK(undo_redo), undoToolButton);
    g_signal_connect(G_OBJECT(exitToolButton), "clicked", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);


    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all(window);

    /* start the main loop, and let it rest until the application is closed */
    gtk_main();

    return 0;
}
